/*

Replication code for Ben Cormier, "Chinese or Western Finance?" forthcoming. Review of International Organizations. 
Note: requires estout package

*/


**************
*Data and set*
**************
use "data1_final.dta", clear
*Declare as time series
encode iso3c, gen(country_num) 
tsset country_num year


******************************************
*Table 1: Descriptives 
*Includes bilateral-only DV, so reflects complete-cases in models of those DVs
******************************************
tabstat ChinaPer_Total ChinaPer_Total_NonIMF ChinaPer_Total_MlatAndOECDbilatO ChinaPer_BilatOnly_OECDOnly HRVtpcy GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal, stat(n mean mi ma sd) case col(stat)


******************************************
*Figure 1: Chinese Borrowing Over Time
******************************************
grstyle init
grstyle set plain, nogrid
bysort year: egen avgChineseFinByYr = mean(ChinaPer_Total)
tsline avgChineseFinByYr, alcolor(black) alpattern(dash) fcolor(none) ///
	title("Figure 1: Average % Borrowed From China Across Countries in Data Over Time", size(small)) subtitle("Against all Other Official Credit Sources (ChnFinance_PerTotal)", size(vsmall)) ///
	xtitle("Year") xtitle(, size(small)) xlabel(2000(2)2017, labsize(small)) ytitle("") ylabel(,labsize(small))


******************************************
*Reset data for main tables and figures
******************************************
use "data1_final.dta", clear
encode iso3c, gen(country_num) 
tsset country_num year

******************************************
*Table 2 and Figure 2
******************************************
*All
fracreg probit ChinaPer_Total l.HRVtpcy
estimates store Model1
fracreg probit ChinaPer_Total l.HRVtpcy GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model2
fracreg probit ChinaPer_Total l.HRVtpcy ChinaPer_Total_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model3
*No IMF
fracreg probit ChinaPer_Total_NonIMF l.HRVtpcy
estimates store Model4
fracreg probit ChinaPer_Total_NonIMF l.HRVtpcy GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model5
fracreg probit ChinaPer_Total_NonIMF l.HRVtpcy ChinaPer_Total_NonIMF_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model6
*No IMF & Only OECD Bilateral
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVtpcy
estimates store Model7
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVtpcy GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model8
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVtpcy v109 GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model9
*OECD Bilateral Only
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVtpcy
estimates store Model10
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVtpcy GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model11
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVtpcy ChinaPer_BilatOnly_OECDOnly_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model12

*Table 2
esttab Model1 Model2 Model3 Model4 Model5 Model6 Model7 Model8 Model9 Model10 Model11 Model12 using ModelsTable_1Main.csv, replace /// 
varlabels("l.HRVtpcy" "Transparency" "ChinaPer_Total_Lag" "DVlag" "ln_CHNLoans_CurrUSD_AllFlows_Lag" "DVlag" "v109" "DVlag" "ChinaPer_Total_NonIMF_Lag" "DVlag" "ChinaPer_BilatOnly_OECDOnly_Lag" "DVlag" ///
"ltDummy" "BondDummy" "SovDebtCrisis_Continuous" "SovDebtCrisis" "PastPTAwChina" "PTAwChn" "taiwanrecgonition" "TaiwanRecog" ///
"UNagreeWithUSA" "agreeUS" "v2x_polyarchy" "Democracy" "v2xnp_regcorr" "Corruption" "TariffMeanImputed_AvgOrReal" "TariffRate") ///
collabels(none) mgroups("DV: All" "DV: No IMF" "DV: No IMF, OECD Bilateral Only" "DV: OECD Bilateral Only", pattern (1 0 0 1 0 0 1 0 0 1 0 0)) ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend /// 
title("Table 2: Transparency Effect on % Chinese Finance (original HRV only)") /// 
note("Standard Errors Robust to Unit Clustering")

*
*Model 3 predicted margins
fracreg probit ChinaPer_Total l.HRVtpcy ChinaPer_Total_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_AllRaw
gen in_model = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_AllRaw HRVtpcy, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(0(.1).3,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 3 (All Official Credit Sources)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show density of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVtpcy if in_model==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations", size(small) axis(2)))


*Model 6
fracreg probit ChinaPer_Total_NonIMF l.HRVtpcy ChinaPer_Total_NonIMF_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_RawNoIMF
gen in_modelIMF = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_RawNoIMF HRVtpcy, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(0(.1).3,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 6 (No IMF)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show density of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVtpcy if in_modelIMF==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations", size(small) axis(2)))

	
*Model 9	
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVtpcy v109 GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_RawNoIMFOECDonly
gen in_modelIMFOECD = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_RawNoIMFOECDonly HRVtpcy, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(0(.1).3,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 9 (OECD Bilat & Non-IMF Multilat)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show density of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVtpcy if in_modelIMFOECD==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations", size(small) axis(2)))

	
*Model 12	
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVtpcy ChinaPer_BilatOnly_OECDOnly_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_RawBilatOnly
gen in_modelBilat = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_RawBilatOnly HRVtpcy, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 12 (OECD Bilateral Only)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show density of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVtpcy if in_modelBilat==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations", size(small) axis(2)))


******************************************
*Clear stored estimates, reset data
******************************************
use "data1_final.dta", clear
encode iso3c, gen(country_num) 
tsset country_num year
	
******************************************
*Table 3 and Figure 3
******************************************
*All
fracreg probit ChinaPer_Total l.HRVImputed_AvgOrReal
estimates store Model13
fracreg probit ChinaPer_Total l.HRVImputed_AvgOrReal GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model14
fracreg probit ChinaPer_Total l.HRVImputed_AvgOrReal ChinaPer_Total_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model15
*No IMF
fracreg probit ChinaPer_Total_NonIMF l.HRVImputed_AvgOrReal
estimates store Model16
fracreg probit ChinaPer_Total_NonIMF l.HRVImputed_AvgOrReal GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model17
fracreg probit ChinaPer_Total_NonIMF l.HRVImputed_AvgOrReal ChinaPer_Total_NonIMF_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model18
*No IMF & Only OECD Bilateral
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVImputed_AvgOrReal
estimates store Model19
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVImputed_AvgOrReal GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model20
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVImputed_AvgOrReal v109 GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model21
*OECD Bilateral Only
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVImputed_AvgOrReal
estimates store Model22
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVImputed_AvgOrReal GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model23
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVImputed_AvgOrReal ChinaPer_BilatOnly_OECDOnly_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
estimates store Model24


*Table
esttab Model13 Model14 Model15 Model16 Model17 Model18 Model19 Model20 Model21 Model22 Model23 Model24 using ModelsTable_2Main.csv, replace /// 
varlabels("l.HRVImputed_AvgOrReal" "Transparency" "ChinaPer_Total_Lag" "DVlag" "ln_CHNLoans_CurrUSD_AllFlows_Lag" "DVlag" "v109" "DVlag" "ChinaPer_Total_NonIMF_Lag" "DVlag" "ChinaPer_BilatOnly_OECDOnly_Lag" "DVlag" ///
"ltDummy" "BondDummy" "SovDebtCrisis_Continuous" "SovDebtCrisis" "PastPTAwChina" "PTAwChn" "taiwanrecgonition" "TaiwanRecog" ///
"UNagreeWithUSA" "agreeUS" "v2x_polyarchy" "Democracy" "v2xnp_regcorr" "Corruption" "TariffMeanImputed_AvgOrReal" "TariffRate") ///
collabels(none) mgroups("DV: All" "DV: No IMF" "DV: No IMF, OECD Bilateral Only" "DV: OECD Bilateral Only", pattern (1 0 0 1 0 0 1 0 0 1 0 0)) ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend /// 
title("Table 3: Transparency Effect on % Chinese Finance (Imputed 2011-2017 HRV)") /// 
note("Standard Errors Robust to Unit Clustering")


*Model 15 predicted margins
fracreg probit ChinaPer_Total l.HRVImputed_AvgOrReal ChinaPer_Total_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_ImputedAll
gen in_model = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_ImputedAll HRVImputed_AvgOrReal, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 15 (All Official Credit Sources)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show percentage of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVImputed_AvgOrReal if in_model==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations" "(with Imputed Values)", size(small) axis(2)))


*Model 18
fracreg probit ChinaPer_Total_NonIMF l.HRVImputed_AvgOrReal ChinaPer_Total_NonIMF_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_ImputedNoIMF
gen in_modelImpIMF = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_ImputedNoIMF HRVImputed_AvgOrReal, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 18 (No IMF)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show percentage of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVImputed_AvgOrReal if in_modelImpIMF==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations" "(with Imputed Values)", size(small) axis(2)))

	
*Model 21	
fracreg probit ChinaPer_Total_MlatAndOECDbilatO l.HRVImputed_AvgOrReal v109 GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_ImputedNoIMFOECDonly
gen in_modelImpIMFOECDonly = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_ImputedNoIMFOECDonly HRVImputed_AvgOrReal, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 21 (OECD Bilat & Non-IMF Multilat)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show percentage of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVImputed_AvgOrReal if in_modelImpIMFOECDonly==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations (with Imputed Values)", size(small) axis(2)))

	
*Model 24	
fracreg probit ChinaPer_BilatOnly_OECDOnly l.HRVImputed_AvgOrReal ChinaPer_BilatOnly_OECDOnly_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy UNagreeWithUSA v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year, vce(cluster country_num)
predict yhat_ImputedBilatOnly
gen in_modelBilatOECDonly = e(sample)
grstyle init
grstyle set plain, nogrid
*Plot only across values of transparency observed in the sample
twoway (lfitci yhat_ImputedBilatOnly HRVImputed_AvgOrReal, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("% Borrowed From China") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Transparency") xtitle(, size(small)) xlabel(-1.5(1.5)7.5, labsize(small))  ///
	legend(off) title("Model 24 (OECD Bilateral Only)") title(, size(small)) ///
	note("Line is predicted marginal effect with 95% Confidence Interval" ///
	"Grey boxes show percentage of Transparency scores across distribution" "of scores observed in sample", size(small))) ///
	(histogram HRVImputed_AvgOrReal if in_modelBilatOECDonly==1, density fcolor(none) lcolor(gs12) yaxis(2) ylabel(,labsize(small) axis(2)) ytitle("Density of Transparency Observations" "(with Imputed Values)", size(small) axis(2)))

	
******************************************
*Clear stored estimates, reset data
******************************************
use "data1_final.dta", clear
encode iso3c, gen(country_num) 
tsset country_num year

	
******************************************
*Table 4 and Figure 4
******************************************
*Create US friends variable, where top half of US agree scores =1 and bottom half of US agree scores =0
*Bilateral drop in N leaves too-few joint groups for hierarchical REs to be identified with continuous UN voting measure; so this proxies for the same effect but allows identification
gen UNUStopHalfDummy = 1 if UNagreeWithUSA>.326
replace UNUStopHalfDummy = 0 if missing(UNUStopHalfDummy)


*Country RE nested in UN Voting alignment level (levelvar is a variable identifying the group structure for the random effects at that level-- cluster of REs based on group within which they are) (and cluster errors at highest level variable groups)
mixed ChinaPer_Total l.HRVtpcy ChinaPer_Total_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNagreeWithUSA: ||country_num: , covariance(unstructured) vce(cluster UNagreeWithUSA)
estimates store Model25
margins, dydx(l.HRVtpcy) atmeans
estimates store ame25
mixed ChinaPer_Total_NonIMF l.HRVtpcy ChinaPer_Total_NonIMF_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNagreeWithUSA: ||country_num: , covariance(unstructured) vce(cluster UNagreeWithUSA)
estimates store Model26
margins, dydx(l.HRVtpcy) atmeans
estimates store ame26
mixed ChinaPer_Total_MlatAndOECDbilatO l.HRVtpcy v109 GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNagreeWithUSA: ||country_num: , covariance(unstructured) vce(cluster UNagreeWithUSA)
estimates store Model27
margins, dydx(l.HRVtpcy) atmeans
estimates store ame27
mixed ChinaPer_BilatOnly_OECDOnly l.HRVtpcy ChinaPer_BilatOnly_OECDOnly_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNUStopHalfDummy: ||country_num: , covariance(unstructured) vce(cluster UNUStopHalfDummy)
estimates store Model28
margins, dydx(l.HRVtpcy) atmeans
estimates store ame28
*Country RE nested in UN Voting alignment level, imputed (HRVImputed_AvgOrReal)
mixed ChinaPer_Total l.HRVImputed_AvgOrReal ChinaPer_Total_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNagreeWithUSA: ||country_num: , covariance(unstructured) vce(cluster UNagreeWithUSA)
estimates store Model29
margins, dydx(l.HRVImputed_AvgOrReal) atmeans
estimates store ame29
mixed ChinaPer_Total_NonIMF l.HRVImputed_AvgOrReal ChinaPer_Total_NonIMF_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNagreeWithUSA: ||country_num: , covariance(unstructured) vce(cluster UNagreeWithUSA)
estimates store Model30
margins, dydx(l.HRVImputed_AvgOrReal) atmeans
estimates store ame30
mixed ChinaPer_Total_MlatAndOECDbilatO l.HRVImputed_AvgOrReal v109 GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNagreeWithUSA: ||country_num: , covariance(unstructured) vce(cluster UNagreeWithUSA)
estimates store Model31
margins, dydx(l.HRVImputed_AvgOrReal) atmeans
estimates store ame31
mixed ChinaPer_BilatOnly_OECDOnly l.HRVImputed_AvgOrReal ChinaPer_BilatOnly_OECDOnly_Lag GDPpcap GDPgrowth ltDummy SovDebtCrisis_Continuous ln_ImpsFromChn ln_ExpsToChn PastPTAwChina taiwanrecognition OilDummy v2x_polyarchy v2xnp_regcorr Left TariffMeanImputed_AvgOrReal i.year ||UNUStopHalfDummy: ||country_num: , covariance(unstructured) vce(cluster UNUStopHalfDummy)
estimates store Model32
margins, dydx(l.HRVImputed_AvgOrReal) atmeans
estimates store ame32


*Table
esttab Model25 Model26 Model27 Model28 Model29 Model30 Model31 Model32 using ModelsTable_3Main.csv, replace /// 
varlabels("l.HRVImputed_AvgOrReal" "Transparency" "ChinaPer_Total_Lag" "DVlag" "ln_CHNLoans_CurrUSD_AllFlows_Lag" "DVlag" "v109" "DVlag" "ChinaPer_Total_NonIMF_Lag" "DVlag" "ChinaPer_BilatOnly_OECDOnly_Lag" "DVlag" ///
"ltDummy" "BondDummy" "SovDebtCrisis_Continuous" "SovDebtCrisis" "PastPTAwChina" "PTAwChn" "taiwanrecgonition" "TaiwanRecog" ///
"UNagreeWithUSA" "agreeUS" "v2x_polyarchy" "Democracy" "v2xnp_regcorr" "Corruption" "TariffMeanImputed_AvgOrReal" "TariffRate") ///
collabels(none) mgroups("HRV Real Only" "HRV Imputed", pattern (1 0 0 0 1 0 0 0)) ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend /// 
title("Table 4: Transparency Effect on % Chinese Finance (Hierarchical Random Effect Models") /// 
note("Standard Errors Robust to Unit Clustering on agreeUS")


**Plot AMEs
set scheme s1mono
grstyle init
grstyle set plain, nogrid 
coefplot ame25 ame26 ame27 ame28 ame29 ame30 ame31 ame32, asequation swapnames rename(HRVImputed_AvgOrReal = HRVtpcy) keep(*HRVtpcy *HRVImputed_AvgOrReal) ///
legend(label(2 "Model 25") label(4 "Model 26") label(6 "Model 27") label(8 "Model 28") label(10 "Model 29") label(12 "Model 30") label(14 "Model 31") label(16 "Model 32")) ///
ylabel(,nogrid) xlabel(none, tlength(0) nogrid) xscale(lstyle(none)) ///
vertical yline(0, lc(black)) mcolor(mono) legend(size(small) position(6) cols(4) bplacement(seast)) levels(95) ///
title("Fig. 4: Average Marginal Effect of Transparency in Table 4 Models", size(mdeiumsmall)) note("95% confidence intervals", size(small) position(7))
